/**
 * 
 */
package net.loyintean.pearlshell.logcenter.jms.logger;

import javax.jms.JMSException;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import net.loyintean.pearlshell.logcenter.jms.IJmsSender;
import net.loyintean.pearlshell.logcenter.jms.IJmsSenderFactory;
import net.loyintean.pearlshell.logcenter.model.IConfigFactory;
import net.loyintean.pearlshell.logcenter.model.ILogModel;
import net.loyintean.pearlshell.logcenter.model.impl.ILogFactory;

/**
 * 利用jms记录日志的appender。
 * <p>
 * 这个日志记录器固定到一个jms消息队列。这个消息队列由url和destinationName指定。
 * 
 * @author Administrator
 * 
 */
public class FixedJmsAppender extends AppenderSkeleton {

	/**
	 * 系统名称
	 */
	private String systemName;

	/**
	 * 日志信息发送器
	 */
	private IJmsSender sender;

	private String url;

	private String destinationName;

	private String userName;

	private String passWord;

	private void init() {
		IJmsSenderFactory factory = IJmsSenderFactory.getFactory("FIXED");

		// sender = factory.create(IConfigFactory.build(systemName, url,
		// destinationName));

		sender = factory.create(IConfigFactory.FACTORY.getFactory()
				.setSystemName(systemName).setUrl(url)
				.setDestinationName(destinationName).setUserName(userName)
				.setPassWord(passWord).build());

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.log4j.Appender#close()
	 */
	@Override
	public void close() {
		try {
			sender.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.log4j.Appender#requiresLayout()
	 */
	@Override
	public boolean requiresLayout() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent
	 * )
	 */
	@Override
	protected void append(LoggingEvent event) {
		ILogModel logModel = ILogFactory.build(systemName, event);

		if (null == sender) {
			init();
		}

		try {
			sender.send(logModel);
		} catch (JMSException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @param systemName
	 *            the systemName to set
	 */
	public void setSystemName(String systemName) {
		this.systemName = systemName;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @param destinationName
	 *            the destinationName to set
	 */
	public void setDestinationName(String destinationName) {
		this.destinationName = destinationName;
	}

	/**
	 * @return the systemName
	 */
	public String getSystemName() {
		return systemName;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @return the destinationName
	 */
	public String getDestinationName() {
		return destinationName;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the passWord
	 */
	public String getPassWord() {
		return passWord;
	}

	/**
	 * @param passWord
	 *            the passWord to set
	 */
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
}
